package com.at.controller.admin;

import com.at.domain.User;
import com.at.service.UserService;
import com.at.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author : code1997
 * @date :2020-09-2020/9/27 19:49
 */
@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping
    public String loginPage(){
        return "admin/login";
    }

    @PostMapping("/login")
    public String login(String username, String password, HttpSession session, RedirectAttributes attributes){
        System.out.println("开始执行登陆验证：：："+username+password);
        User user = userService.checkUser(username,password);
        System.out.println(user+"========");
        if (user==null){
            attributes.addFlashAttribute("message", "用户名或密码错误");
            attributes.addFlashAttribute("username", username);
            return "redirect:/admin/";
        }
        user.setPassword(null);
        session.setAttribute("user", user);
        return "admin/index";
    }
    @GetMapping("/logout")
    public String logout(HttpSession session){
        System.out.println("退出登陆++++++++");
        session.removeAttribute("user");
        return "redirect:/admin/";
    }


}
